<?php
namespace model;

class Login{
	
		private static $username = 'username';
		private static $password = 'password';	
		private static $cryptPassword = 'Password';
		private static $salt = "salt";
		
	public function checkCookies(){
		
		if ($this->checkTimeOnCookie() == FALSE){
			$message = "Cookie to old!";
			$this->destroyCookie();
			return $message;
		}
		
		if($_COOKIE['username'] == "Admin" && $_COOKIE['password'] == md5("Password" . self::$salt)){
			$message = 'Inloggning lyckades med cookies nomnomnom';
			$this->createSession();
			
		}else{
			$this->destroyCookie();
			$message = "Fel information i cookies ";
		}
		return $message;
	}
	
	public function tryLogIn(){
		try{
				if ($_POST[self::$username] == "Admin" && $_POST[self::$password] == "Password" ){
						$message = 'Inloggning lyckades';
					if(isset($_POST['checkbox'])){
						$message .= ' och dina uppgifter är sparade';
						$this->setCookies();
					}
					$this->createSession();
					return $message;
				}else{
					if(empty($_POST[self::$username]) && empty($_POST[self::$password])){
						$message = 'Användarnamn saknas';
					}
					else if(empty($_POST[self::$username])){
						$message = 'Användarnamn saknas!';
					}
					else if(empty($_POST[self::$password])){
						$message = 'Lösenord saknas';
					}
					else{
						$message = 'Felaktigt användarnamn och/eller lösenord';
					}
					return $message;
				}
		}catch(exception $e){echo"Something wrong in model";}
	}
	
	//logs out || destroys sesseions and cookies
	//return string
	public function logOut(){
			
			$this->destroyCookie();
			unset($_SESSION['loggedIn']);
			session_destroy();
			$message = 'Du har loggats ut';
		    return $message;
		}
	//set cookies
	private function setCookies(){
		$cookieTimer = time() + 200;
		file_put_contents("endtime.txt", "$cookieTimer");
		setcookie("username", $_POST[self::$username] ,$cookieTimer);
		self::$cryptPassword = md5($_POST[self::$password] . self::$salt);
		setcookie("password", self::$cryptPassword ,$cookieTimer);
	} 
	
	private function setDummyCookie(){
		self::$cryptPassword = crypt("Password");
	}
	private function checkTimeOnCookie(){
		$cookieEndTime = file_get_contents("endtime.txt");
		try{
			if(time() < $cookieEndTime){
				return true;
			}else if (time() > $cookieEndTime){
				return false;
			}
		}catch(exception $e){
			echo "Something wrong in model";
		}
		
	}
	private function destroyCookie(){
		setcookie('username','',time()-3600);
		setcookie('password','',time()-3600);
	}
	
	private function createSession(){
		$_SESSION['loggedIn'] = true;
		
		$_SESSION["session_security"] = array();
		$_SESSION["session_security"]["webbläsare"] = $this->getUserAgent();
	}
	
	public function checkSession(){
		if($_SESSION["session_security"]["webbläsare"] == $this->getUserAgent()){
			return $message = "";
		}else {
			$this->destroyCookie();
			unset($_SESSION['loggedIn']);
			session_destroy();
			return $message = "Sessions Tjuv!";
		}		
	}
	
	
	// Magic happens in this function to find out the users browser
	//http://stackoverflow.com/questions/9693574/user-agent-extract-os-and-browser-from-string-php
	public static function getUserAgent()
{
    static $agent = null;

    if ( empty($agent) ) {
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if ( stripos($agent, 'Firefox') !== false ) {
            $agent = 'firefox';
        } elseif ( stripos($agent, 'MSIE') !== false ) {
            $agent = 'ie';
        } elseif ( stripos($agent, 'iPad') !== false ) {
            $agent = 'ipad';
        } elseif ( stripos($agent, 'Android') !== false ) {
            $agent = 'android';
        } elseif ( stripos($agent, 'Chrome') !== false ) {
            $agent = 'chrome';
        } elseif ( stripos($agent, 'Safari') !== false ) {
            $agent = 'safari';
        } elseif ( stripos($agent, 'AIR') !== false ) {
            $agent = 'air';
        } elseif ( stripos($agent, 'Fluid') !== false ) {
            $agent = 'fluid';
        }

    }

    return $agent;
}
}





